//
//  LYYTabBar.m
//  LYYWeibo
//
//  Created by lanou3g on 15/10/10.
//  Copyright (c) 2015年 李岩岩. All rights reserved.
//

#import "LYYTabBar.h"
#import "LYYTabBarButton.h"

@interface LYYTabBar ()

@property (nonatomic, strong) NSMutableArray *taBarButtons;
@property (nonatomic, weak) LYYTabBarButton *selectedButton;
@property (nonatomic, weak) UIButton *plusButton;

@end

@implementation LYYTabBar
- (NSMutableArray *)taBarButtons{
    if (_taBarButtons == nil) {
        _taBarButtons = [NSMutableArray array];
        
    }
    return _taBarButtons;
}
- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

        //添加一个脚印按钮
        self.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1.0];
        
        UIButton *plusButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [plusButton setBackgroundImage:[UIImage imageNamed:@"cha"] forState:UIControlStateNormal];
        [plusButton setBackgroundImage:[UIImage imageNamed:@"chax"] forState:UIControlStateHighlighted];
        [plusButton setImage:[UIImage imageNamed:@"cha"] forState:UIControlStateNormal];
        [plusButton setImage:[UIImage imageNamed:@"chax"] forState:UIControlStateHighlighted];
        plusButton.bounds = CGRectMake(0, 0, plusButton.currentBackgroundImage.size.width, plusButton.currentBackgroundImage.size.height);
        //脚印按钮的监听事件
        [plusButton addTarget:self action:@selector(plusButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:plusButton];
        self.plusButton = plusButton;
        
    }
    return self;
}
/**
 *脚印按钮的监听事件
 */
- (void)plusButtonClick{
    //model形式弹出控制器
    if ([self.delegate respondsToSelector:@selector(tabBardidClickedPlusButton:)]) {
        [self.delegate tabBardidClickedPlusButton:self];
    }
}

- (void)addTabBarButtonWithItem:(UITabBarItem *)item
{
    // 1.创建按钮
    LYYTabBarButton *button = [[LYYTabBarButton alloc]init];
    [self addSubview:button];
    
    // 添加按钮到数组中
    [self.taBarButtons addObject:button];
    
    
    // 2.设置数据
    button.item = item;
    // 3.监听按钮点击
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    // 4.默认选中第0个按钮
    if (self.taBarButtons.count == 1) {
        [self buttonClick:button];
    }
    
}
/**
 *  监听按钮点击
 */
- (void)buttonClick:(LYYTabBarButton *)button
{

    
    // 1.通知代理
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectedButtonFrom:to:)]) {
        [self.delegate tabBar:self didSelectedButtonFrom:self.selectedButton.tag to:button.tag];
    }
    
    // 2.设置按钮的状态
    self.selectedButton.selected = NO;
    button.selected = YES;
    self.selectedButton = button;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //调整加号按钮的frame
    CGFloat h = self.frame.size.height;
    CGFloat w = self.frame.size.width;
    //center使用时要注意
    self.plusButton.center = CGPointMake(w *0.5, h * 0.5);
    
    
    
    // 按钮的frame数据
    CGFloat buttonH = self.frame.size.height;
    CGFloat buttonW = self.frame.size.width / self.subviews.count;
    CGFloat buttonY = 0;
    
    for (int index = 0; index<self.taBarButtons.count; index++) {
        // 1.取出按钮
        LYYTabBarButton *button = self.taBarButtons[index];
        
        // 2.设置按钮的frame
        CGFloat buttonX = index * buttonW;
        if (index > 1) {
            buttonX += buttonW;
            
        }
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);
        
        // 3.绑定tag
        button.tag = index;
    }
}



@end
